<?php

class ParameterHeaderForm extends CFormModel {
	
	public $mnemonic;
	public $label;
	public $index = -1;
	public $type = 'string';
	public $key = false;
	
	public function __construct($header = array())
	{
		if (is_array($header))
		{
			$this->attributes = $header;
		}
	}
	
}

class ParameterValueForm extends CFormModel {
	
	private $row = array();
	private $index = -1;
	
	public function __construct($index, $headers, $values = array())
	{
		/*Constants::print_var(is_array($values)?"array":"not array");
		if (!is_array($values))
		{
			$values = array();
		}*/
		
		$this->index = $index;
		foreach ($headers as $header)
		{
			if (isset($values[$header['mnemonic']]))
			{
				$this->row[$header['mnemonic']] = $values[$header['mnemonic']];
			}else{
				$this->row[$header['mnemonic']] = $this->getDefaultValueForType($header['type']);
			}
		}
	}
	
	private function getDefaultValueForType($type)
	{
		switch($type)
		{
			case 'integer':
			case 'float':
				return 0;
				
			case 'string':
			case 'text':
				return "";
			
			case 'boolean':
				return false;
				
			default:
				return null;
		}
	}
	
	public function attributeNames()
	{
		return array_keys($this->row);
	}
	
	public function __get($name)
	{
		if (array_key_exists($name, $this->row))
		{
			return $this->row[$name];
		}else{
			return parent::__get($name);
		}
	}
	
	public function __set($name, $value)
	{
		if (array_key_exists($name, $this->row))
		{
			$this->row[$name] = $value;
		}else{
			return parent::__set($name, $value);
		}
	}
	
	public function getIndex()
	{
		return $this->index;
	}
}

class ParameterForm extends CFormModel {

	private $parameter;
	public $param_mnemonic;
	public $param_label;
	public $param_type;
	public $read_only;
	public $headers = array();
	public $values = array();

	public function __construct($parameter = null)
	{
		if ($parameter != null)
		{
			$this->parameter = $parameter;
			$this->param_mnemonic = $parameter->description->param_mnemonic;
			$this->param_label = $parameter->description->param_label;
			$this->param_type = $parameter->description->param_type;
			$this->read_only = $parameter->description->read_only;
			foreach ($parameter->headers as $header)
			{
				$this->headers[$header['index']] = new ParameterHeaderForm($header);
			}
			foreach ($parameter->values as $n=>$values)
			{
				$this->values[$n] = new ParameterValueForm($n, $this->headers, $values);
			}
		}else{
			$this->parameter = new CParameter($this->headers, $this->values);
		}
	}
	
	public function getParameter()
	{
		return $this->parameter;
	}
}

?>
